﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Configuration;
using cecbClasses.Data_Adapters.Login;
using cecbData;

namespace CECB.Dashbord
{
    /// <summary>
    /// Interaction logic for DashSlideSettings_Window.xaml
    /// </summary>
    public partial class DashSlideSettings_Window : Window
    {
        public DashSlideSettings_Window()
        {
            InitializeComponent();
            dataContext = new DSSEntities();
            comboBoxComponents.ItemsSource = new DSSEntities().Components.GroupBy(i => i.cmpt_mainCat).Select(i => i.FirstOrDefault());
            listBoxComponents.ItemsSource = dataContext.Components;
            tbxEmail.Text = new cecbClasses.Data_Adapters.Setting.Func_Settings().getLastMail();
            settingsTab.SelectedIndex = 1;
            ProjectAutocompleteTextBox1.ItemsSource = new cecbClasses.Data_Adapters.Setting.Func_Settings().getAllActiveProjects();
            TextBoxExcelPath.Text = new cecbClasses.Data_Adapters.Setting.Func_Settings().getSettingValues(5);
            MemberAutocompleteTextBox.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getAllMembers();
        }

        DSSEntities dataContext;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (new UserLogin().UserLoginClick(textBox1.Text.Trim(), textBox2.Text.Trim(), true))
            {
                MessageBox.Show(" Password Change successfully  ", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
                MessageBox.Show("Enter Valide UserName And Password", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void textBox5_FocusableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }




        private void textBox3_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox3.Clear();
           
        }

        private void textBox4_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox4.Clear();
        }

        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox1.Clear();
        }

        private void textBox2_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox2.Clear();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SaveButton.Visibility = System.Windows.Visibility.Hidden;
            UpdateButton.Visibility = System.Windows.Visibility.Visible;
            textBoxcomponents.Text = ((Components)listBoxComponents.SelectedItem).cmpt_name.ToString();
            lblname.Text = "Update selected component";
            comboBoxComponents.SelectedItem = (Components)listBoxComponents.SelectedItem;
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            listBoxComponents.ItemsSource = dataContext.Components;
        }

        private void textBoxcomponents_GotFocus(object sender, RoutedEventArgs e)
        {
            if (textBoxcomponents.Text.Length > 0)
            {
                SaveButton.Visibility = System.Windows.Visibility.Hidden;
                UpdateButton.Visibility = System.Windows.Visibility.Visible;
                lblname.Text = "Update selected component";
            }
            else
            {
                SaveButton.Visibility = System.Windows.Visibility.Visible;
                UpdateButton.Visibility = System.Windows.Visibility.Hidden;
                lblname.Text = "Add new component";
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            Components com = (Components)listBoxComponents.SelectedItem;


        }

        private void SaveButtonEmail_Click(object sender, RoutedEventArgs e)
        {
            if (tbxNewEmail.Text.Length == 0 || (!Regex.IsMatch(tbxNewEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
            {
                MessageBox.Show("Enter Valid Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (tbxNewEmailPass.Password.Length == 0)
            {
                MessageBox.Show("Enter Email's Password", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                new cecbClasses.Data_Adapters.Setting.Func_Settings().updateEmailAddress(tbxNewEmail.Text.Trim(), tbxNewEmailPass.Password.Trim());
                MessageBox.Show(" Email Address Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                tbxNewEmail.Clear();
                tbxNewEmailPass.Clear();
                tbxEmail.Text = new cecbClasses.Data_Adapters.Setting.Func_Settings().getLastMail();
            }
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ButtonSaveProject_Click(object sender, RoutedEventArgs e)
        {
            Projects proj = (Projects)ProjectAutocompleteTextBox1.SelectedItem;
            if (proj != null)
            {
                if (checkBoxDeactivate.IsChecked == true)
                {


                    if (new cecbClasses.Data_Adapters.Setting.Func_Settings().uodateProjectActivateDetails(proj.proj_reference))
                    {
                        MessageBox.Show(" Project ' " + proj.proj_name + " ' Deactivate Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        ButtonSaveProject.IsEnabled = false;
                    }
                }
                else {

                    MessageBox.Show(" If you need to deactivate the project tick 'Deactivate'", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }
            else {

                MessageBox.Show(" Select a project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void MemberAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void ButtonFolderPath_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();



            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                TextBoxExcelPath.Text = fbd.SelectedPath;
                ButtonSaveExcelPath.Foreground = Brushes.Red;
            }



        }

        private void ProjectAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if ((Projects)ProjectAutocompleteTextBox1.SelectedItem != null)
            {
                ButtonSaveProject.IsEnabled = true;
            }
            else {
                ButtonSaveProject.IsEnabled = false;
            }
        }

        private void ButtonSaveExcelPath_Click(object sender, RoutedEventArgs e)
        {
            if (TextBoxExcelPath.Text.ToString().Length > 0)
            {
                if (new cecbClasses.Data_Adapters.Setting.Func_Settings().updateSetting(5, TextBoxExcelPath.Text.Trim()))
                {
                     MessageBox.Show("Path save Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            else
            {
                MessageBox.Show("Select a path to save auto genarated excels", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void MemberAutocompleteTextBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if ((Members)MemberAutocompleteTextBox.SelectedItem != null)
            {
                ButtonSaveMemberDeactivate.IsEnabled = true;
            }
            else
            {
                ButtonSaveMemberDeactivate.IsEnabled = false;
            }
        }

        private void ButtonSaveMemberDeactivate_Click(object sender, RoutedEventArgs e)
        {
            Members mem = (Members)MemberAutocompleteTextBox.SelectedItem;
            if (mem != null)
            {

                if (new cecbClasses.Data_Adapters.Member.Func_Members().uodateMemberActivateDetails(mem.mem_reference))
                {
                    MessageBox.Show(" Member ' " + mem.mem_name + " ' Deactivate Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    ButtonSaveMemberDeactivate.IsEnabled = false;
                }

            }
            else
            {

                MessageBox.Show(" Select a Member", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
